<?php
# Sample script for RAWS tutorial 9: see http://rambla.eu/ for more info.
#
# Copyright (C) 2010 rambla.be

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require_once '../RawsClient/Raws/Rass.php';

# Provide the path to a local testfile, that can be uploaded via RASS
define('LOCAL_FILE', '/my/path/to/a/file.mp4');

# Provide your own RASS credentials here
define('USER', 'xxx'); # your user account name
define('PWD', 'xxx'); # your user account pwd
define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")

define('BASE', 'http://rass.' . CDN . '.rambla.be/');

# all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
try {
  # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
  $rass = new Rass(USER, PWD, BASE);
  
  # Create a "tutorial9" directory under the root-directory
  $dir_entry = $rass->createDir("tutorial9", True);
  
  # Upload a file 'eagle.mp4' to your dir and attach some tags to it
  $item_entry = $rass->postItem($dir_entry->path, "eagle.mp4", LOCAL_FILE);
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("animal"), new Rass_Extension_Tag("bird"), new Rass_Extension_Tag("birdwatching")), array());
  
  # Upload a file 'owl.mp4' to your dir and attach some tags to it
  $item_entry = $rass->postItem($dir_entry->path, "owl.mp4", LOCAL_FILE);
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("animal"), new Rass_Extension_Tag("bird"), new Rass_Extension_Tag("documentary")), array());
  
  # Upload a file 'snake.mp4' to your dir and attach some tags to it
  $item_entry = $rass->postItem($dir_entry->path, "snake.mp4", LOCAL_FILE);
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("animal"), new Rass_Extension_Tag("reptile"), new Rass_Extension_Tag("documentary")), array());
  
  # Upload a file 'spiderman.mp4' to your dir and attach some tags to it
  $item_entry = $rass->postItem($dir_entry->path, "spiderman.mp4", LOCAL_FILE);
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("superhero"), new Rass_Extension_Tag("movie"),  new Rass_Extension_Tag("rebirth")), array());
  
  # Upload two test files, without any tags
  $item_entry = $rass->postItem($dir_entry->path, "test1.mp4", LOCAL_FILE);
  $item_entry = $rass->postItem($dir_entry->path, "test2.mp4", LOCAL_FILE);
  
  
  # GET feed
  
  echo "Getting meta entries for files in directory: " . $dir_entry->path . "\n";
  $query = new Rass_MetaQuery("file");
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\nFound " . $entry->kind . " entry with path = " . $entry->path . "\n";
    foreach ($entry->content->params->tag as $t) {
      echo "Tag: " . $t->text . "\n";
    }
  }

  echo "Getting meta entries for files in directory: " . $dir_entry->path . " corresponding to the pattern 'test?.*' \n";
  $query = new Rass_MetaQuery("file");
  $query->setName("test?.*");
  $feed = $rass->getMetaFeed("/", $query);
  foreach ($feed as $entry) {
    echo "\nFound " . $entry->kind . " entry with path = " . $entry->path . "\n";
    foreach ($entry->content->params->tag as $t) {
      echo "Tag: " . $t->text . "\n";
    }
  }
  
  
  # 'tag' searches
  
  echo "\n\nSearch for all files tagged with 'bird' :";
  $query->setSearchArg("tag", array("bird"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }

  echo "\n\nSearch for all files tagged with 'bird' OR 'reptile' :";
  $query->setSearchArg("tag", array("bird", "reptile"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }

  #  with 'animal' AND 'documentary'
  echo "\n\nSearch for all files tagged with 'animal' AND 'documentary' :";
  $query->setSearchArg("tag", array(), array("animal", "documentary"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }

  echo "\n\nSearch for all files tagged with 'animal' BUT NOT with 'bird' :";
  $query->setSearchArg("tag", array("animal"), array(), array("bird"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }
  
  echo "\n\nSearch for all files tagged with 'bird' or 'reptile' BUT NOT with 'animal' :";
  $query->setSearchArg("tag", array("bird","reptile"), array(), array("animal"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }
  
  
  # 'qtag' or 'iqtag' searches

  echo "\n\nCase-sensitive search for all files with a tag that contains the string 'ani' :";
  $query = new Rass_MetaQuery("file");
  $query->setSearchArg("qtag", array("ani"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }

  # qtag is case sensitive
  echo "\n\nCase-sensitive search for all files with a tag that contains the string 'Ani' :";
  $query = new Rass_MetaQuery("file");
  $query->setSearchArg("qtag", array("Ani"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }

  # iqtag is case insensitive
  echo "\n\nCase-insensitive search for all files with a tag that contains the string 'Ani' :";
  $query = new Rass_MetaQuery("file");
  $query->setSearchArg("iqtag", array("Ani"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }
  
  # search operators can also be used with (i)qtag
  echo "\n\nCase-insensitive search for all files with a tag that contains the string 'HERO' and the string 'BIR' :";
  $query = new Rass_MetaQuery("file");
  $query->setSearchArg("iqtag", array(), array("HERO", "BIR"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }
  
  # combined searches are NOT ALLOWED => if set, the tag argument will prevail
  echo "\n\nSearch for all files tagged with 'animal' or 'superhero' (the iqtag argument will be ignored) :";
  $query = new Rass_MetaQuery("file");
  $query->setSearchArg("tag", array("animal", "superhero"));
  $query->setSearchArg("iqtag", array(), array(), array("Document"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }

  # combined searches are NOT ALLOWED => you can unset a search argument
  echo "\n\nSearch for all files that DON'T have a tag containing the string 'Document' (the tag argument has been unset) :";
  $query = new Rass_MetaQuery("file");
  $query->setSearchArg("tag", array("animal", "superhero"));
  $query->unsetSearchArg("tag");
  $query->setSearchArg("iqtag", array(), array(), array("Document"));
  $feed = $rass->getMetaFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
  }
  
  // # DELETE directory on the CDN. Set second argument to True to delete recursively.
  // $rass->deleteDir($dir_entry->path, True);
  // echo "\nDeleted dir: " . $dir_entry->path . "\n";
  


}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
